import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
// @ts-ignore
import store from "@/store/index.js";

Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  // {
  //   path: '/',
  //   //component: HomeView,
  //   component: () => import(/* webpackChunkName: "about" */ '../views/Manage.vue'),
  //   redirect: '/home',
  //   children: [
  //     { path: 'home', name: '首页', component: () => import('../views/Home.vue')},
  //     { path: 'user', name: '用户管理', component: () => import('../views/User.vue')},
  //     { path: 'file', name: '文件管理', component: () => import('../views/File.vue')},
  //     {path: 'person', name: '/个人中心', component: () => import('../views/Person.vue')},
  //     {path: 'password', name: '/修改密码',component: () => import('../views/Password.vue')},
  //     {path: 'role', name: '角色管理', component: () => import('../views/Role.vue')},
  //     {path: 'menu', name: '菜单管理', component: () => import('../views/Menu.vue')},
  //   ]
  // },

  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/front/Front.vue'),
    children: [
        { path: 'home', name: 'FrontHome', component: () => import('../views/front/Home.vue')},
        { path: 'item1', name: 'Item1', component: () => import('../views/front/Item1.vue')},
        {path: 'person', name: 'Person', component: () => import('../views/front/Person.vue')},
        {path: 'password', name: 'Password', component: () => import('../views/front/Password.vue')},
        {path: 'video', name: 'Video', component: () => import('../views/front/Video.vue')},
        {path: 'videoDetail', name: 'VideoDetail', component: () => import('../views/front/VideoDetail.vue')},
        {path: 'article', name: 'FrontArticle', component: () => import('../views/front/Article.vue')},
        {path: 'articleDetail', name: 'FrontArticleDetail', component: () => import('../views/front/ArticleDetail.vue')}
    ]

  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
  // @ts-ignore
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

// 注意，刷新页面会导致页面路由重置
export  const  setRoutes = () => {
  const storeMenus = localStorage.getItem("menus")
  if (storeMenus) {
    // 获取当前的路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    console.log("当前路由",currentRouteNames)
    if (!currentRouteNames.includes("Manage")) {
      // 拼接动态路由
      const manageRoute = { path: '/', name: 'Manage', component: () => import('../views/Manage.vue'), redirect: "/home", children: [
          { path: 'person', name: '个人信息', component: () => import('../views/Person.vue')},
          { path: 'password', name: '修改密码', component: () => import('../views/Password.vue')}
        ]}
    const menus = JSON.parse(storeMenus)
    menus.forEach((item: { name: string; path: string; pagePath: string; children:[]}) => {
      if (item.path) { // 当且仅当path不为空的时候才去设置路由
        let itemMenu = { path: item.path.replace("/", ""),name: item.name,component: () => import('../views/' + item.pagePath + '.vue')}
        manageRoute.children.push(itemMenu)
        //debugger
      } else if (item.children.length)  {
        item.children.forEach((child : { name: string; path: string; pagePath: string;}) => {
          if (child.path){
            let itemMenu = { path: child.path.replace("/", ""), name: child.name, component: () => import('../views/' + child.pagePath + '.vue')}
            manageRoute.children.push(itemMenu)
          }
        })
      }
    })
      // 动态的添加路由到现在的路由对象中去
      router.addRoute(manageRoute)
    }
  }
}

// 重置我就再set一次路由
setRoutes()

// 路由守卫
router.beforeEach((to, from, next) => {

  if (typeof to.name === "string") {
    localStorage.setItem("currentPathName", to.name)
    // 设置当前的路由名称，为了在Header组件中去使用
    store.commit("setPath")  // 触发store的数据更新
  }
  // 未找到路由的情况
  if (!to.matched.length) {
    //debugger
    const storeMenus = localStorage.getItem("menus")
    if (storeMenus) {
      next("/404")
    } else {// 没有登录的情况
      // 跳回到登录页面
      next("/login")
    }
  }
  next()
})
export default router
